//------------------------------------------------------------------------------
#include <QSqlRecord>
#include <QSqlQuery>
#include <QtDebug>
#include <QInputDialog>
#include "CGestionFavorisProspectFrm.h"
#include "CMyTreeWidgetItemData.h"
#include "CMySQL.h"
//------------------------------------------------------------------------------
CGestionFavorisProspectFrm::CGestionFavorisProspectFrm(QWidget *parent) : CGestionFavorisFrm(parent)
{
	QStringList title;
	title << tr("Favoris");
	
	treeFavoris->setColumnWidth(0, 1000);
	treeFavoris->setHeaderLabels(title);
	
	loadFavoris(0);
	
}
//------------------------------------------------------------------------------
void CGestionFavorisProspectFrm::loadFavoris(QTreeWidgetItem * item, int * parent) {
	QString requete;
	QSqlQuery *query;
	
	requete="SELECT fp.id, fp.prospect, fp.nom AS folderName, p.nom AS prospectName, fp.parent ";
	requete+="FROM favorisProspect AS fp ";
	requete+="LEFT JOIN prospect AS p ON p.code=fp.prospect ";
	requete+="WHERE user=\""+CMySQL::getInstance()->getUser()+"\" AND ";
	requete+="parent "+(parent == 0 ? "IS NULL" : "= "+QString::number(*parent))+" ";
	requete+="ORDER BY prospect";
	
	if(CMySQL::getInstance()->execQuery(requete, &query)) {
		while(query->next()) {
			QSqlRecord rec=query->record();
			QStringList infos;
			QTreeWidgetItem *nItem=item;
			CMyTreeWidgetItemData data(	rec.value("id").toInt(),
										rec.value("prospect").isNull() ? 
										CMyTreeWidgetItemData::dtFolder : 
										CMyTreeWidgetItemData::dtBookmark);
			
			if(rec.value("prospect").isNull()) {
				int p=rec.value("id").toInt();
				
				if(!rec.value("parent").isNull()) {
					infos << rec.value("folderName").toString();
					
					nItem=new QTreeWidgetItem(infos);
					
					nItem->setIcon(0, QIcon(":coMexpert/images/folder.png"));
					nItem->setData(0, Qt::UserRole, QVariant::fromValue(data));	
					
					item->addChild(nItem);
				}else {
					nItem=createRacine(p);
				}
				
				loadFavoris(nItem, &p);
			}else {
				infos << rec.value("prospectName").toString();
				
				nItem=new QTreeWidgetItem(infos);
				nItem->setData(0, Qt::UserRole, QVariant::fromValue(data));
				
				item->addChild(nItem);
			}
		}
	}
	delete query;
}
//------------------------------------------------------------------------------
void CGestionFavorisProspectFrm::dropFavorisFromBase(int id) {
	QSqlQuery *query;
	QString requete="SELECT id FROM favorisProspect WHERE parent="+QString::number(id);
	
	if(CMySQL::getInstance()->execQuery(requete, &query)) {
		while(query->next()) {
			dropFavorisFromBase(query->record().value("id").toInt());
		}
	}
	
	requete="DELETE FROM favorisProspect WHERE id="+QString::number(id);
	
	CMySQL::getInstance()->execQuery(requete);
	
	delete query;
}
//------------------------------------------------------------------------------
void CGestionFavorisProspectFrm::on_cmdSupprimer_clicked(void)
{
	QTreeWidgetItem * item=treeFavoris->currentItem();
	if(item != 0) {
		CMyTreeWidgetItemData data=qvariant_cast<CMyTreeWidgetItemData>(item->data(0, Qt::UserRole));
		
		QMessageBox msgBox(	QMessageBox::Question, tr("Confirmation"), 
							tr("Etes vous sûre de vouloir supprimer le favoris / dossier de favoris ?"), 
							QMessageBox::Yes | QMessageBox::No);
							
		msgBox.exec();
		
		if(msgBox.result() == QMessageBox::Yes)
		{
			dropFavorisFromBase(data.getId());
			
			dropFavoris(item);
			treeFavoris->removeItemWidget(item, 0);
			delete item;
		}
	}
}
//------------------------------------------------------------------------------
void CGestionFavorisProspectFrm::on_cmdAjoutDossier_clicked(void) {
	QTreeWidgetItem *item=treeFavoris->currentItem();
	
	if(item == 0) {
		if(treeFavoris->topLevelItemCount() != 0) {
			item=treeFavoris->topLevelItem(0);
		}else {
			QVariant lastInsertId;
			
			QString requete="INSERT INTO favorisProspect (user) SELECT \""+CMySQL::getInstance()->getUser()+"\"";
			CMySQL::getInstance()->execQuery(requete, true, &lastInsertId);
			
			item=createRacine(lastInsertId.toInt());
		}
	}
	
	CMyTreeWidgetItemData data=qvariant_cast<CMyTreeWidgetItemData>(item->data(0, Qt::UserRole));
	
	if(data.getType() == CMyTreeWidgetItemData::dtFolder || data.getType() == CMyTreeWidgetItemData::dtRoot) {
		bool ok;
		QString text=QInputDialog::getText(this, tr("Nom du dossier"), tr("Nom du dossier"), QLineEdit::Normal, "", &ok);
		
		if(ok && !text.isEmpty()) {
			QVariant lastInsertId;
			QStringList infos;
			
			infos << text;
			
			QString requete="INSERT INTO favorisProspect (user, nom, parent) SELECT \""+CMySQL::getInstance()->getUser()+"\", \""+text+"\", "+QString::number(data.getId());
			
			if(CMySQL::getInstance()->execQuery(requete, true, &lastInsertId)) {
			
				QTreeWidgetItem *nItem=new QTreeWidgetItem(infos);
				CMyTreeWidgetItemData data(lastInsertId.toInt(), CMyTreeWidgetItemData::dtFolder);
				
				nItem->setIcon(0, QIcon(":coMexpert/images/folder.png"));
				nItem->setData(0, Qt::UserRole, QVariant::fromValue(data));
				
				item->addChild(nItem);
			}
		}
	}
}
//------------------------------------------------------------------------------
void CGestionFavorisProspectFrm::on_treeFavoris_itemDroped(int sourceId, int destId) {
	QString requete="UPDATE favorisProspect SET parent="+QString::number(destId)+" WHERE id="+QString::number(sourceId);
	
	CMySQL::getInstance()->execQuery(requete);
}
//------------------------------------------------------------------------------